
app.controller('editContractCtrl', function($element, $scope, $state, rest, user, uploadConf, tipService, $window, $rootScope) {
	$scope.contractDatas = angular.copy($scope.ngDialogData, {});
	var token = $window.localStorage.getItem("token");
	
    $scope.hideBtn = function(){
    	$scope.closeThisDialog();
    }
    
    //日期格式转换
    var parserDate = function (date) {  
	    var t = Date.parse(date);  
	    if (!isNaN(t)) {  
	        return new Date(Date.parse(date.replace(/-/g, "/")));  
	    } else {  
	        return new Date();  
	    }  
	};
    
    //保存
    $scope.editSave = function(){
    	
    	var _data = {
    		id: $scope.contractDatas.id,
    		tenantName: $scope.contractDatas.tenantName,
    		tenantPhone: $scope.contractDatas.tenantPhone,
    		tenantIdCard: $scope.contractDatas.tenantIdCard,
    		tenantbank: $scope.contractDatas.tenantbank,
    		beforPeoplePhone: $scope.contractDatas.beforPeoplePhone,
    		urgentPeopleName: $scope.contractDatas.urgentPeopleName,
    		urgentPeplePhone: $scope.contractDatas.urgentPeplePhone,
    		communityName: $scope.contractDatas.communityName,
    		detailedAddress: $scope.contractDatas.detailedAddress,
    		fixationStartTime: new Date($scope.startDate1),
    		fixationEndTime: new Date($scope.overDate1),
    		flexibleStartTime: new Date($scope.startDate2),
    		flexibleEndTime: new Date($scope.overDate2),
    		leaseStartTime: new Date($scope.startDate3),
    		leaseEndTime: new Date($scope.overDate3),
    		//rentDay: '',
    		validRentDay: $scope.contractDatas.validRentDay,
    		paymentType: $scope.contractDatas.paymentType,
    		depositCharges: $scope.contractDatas.depositCharges,
    		rentCharges: $scope.contractDatas.rentCharges,
    		totalAmount: $scope.contractDatas.totalAmount
    	}
    	
    	rest.decoJava({
    		type: "post",
            url: "/tenancy-sys/contract-resource/contract/edit",
            data: _data,
            beforeSend: function(request) {
                request.setRequestHeader("token", token);
            },
            success: function(res) {
                layer.closeAll('loading');
                if(res.code == "0000"){
                	layer.msg(res.message, {
			        	offset:'t',
			        	icon: 1
					});
					$scope.closeThisDialog();
                } else{
                	layer.msg(res.message,{
	    				offset:'t'
	    			});
                }
            }
    	})
    }
    
    //日期选择控件
    $scope.today = function() {
        $scope.startDate1 = $scope.contractDatas.fixationStartTime;  //固定租期开始
        $scope.overDate1 = $scope.contractDatas.fixationEndTime;  //固定租期结束
        $scope.startDate2 = $scope.contractDatas.flexibleStartTime;  //固定租期开始
        $scope.overDate2 = $scope.contractDatas.flexibleEndTime;  //固定租期结束
        $scope.startDate3 = $scope.contractDatas.leaseStartTime;  //租期总长开始
        $scope.overDate3 = $scope.contractDatas.leaseEndTime;  //租期总长结束
    };

    $scope.closeAll = function() {
        $scope.beginDated1 = false;
        $scope.endDated1 = false;
        $scope.beginDated2 = false;
        $scope.endDated2 = false;
        $scope.beginDated3 = false;
        $scope.endDated3 = false;
    }
	
	//触发固定日期开始控件
	$scope.beginDate1 = function($event){
		$event.preventDefault();
        $event.stopPropagation();
        $scope.closeAll();
        $scope.beginDated1 = true;
	}
	//触发固定日期结束控件
	$scope.endDate1 = function($event){
		$event.preventDefault();
        $event.stopPropagation();
        $scope.closeAll();
        $scope.endDated1 = true;
	}
	
    //触发灵活日期开始控件
	$scope.beginDate2 = function($event){
		$event.preventDefault();
        $event.stopPropagation();
        $scope.closeAll();
        $scope.beginDated2 = true;
	}
	//触发灵活日期结束控件
	$scope.endDate2 = function($event){
		$event.preventDefault();
        $event.stopPropagation();
        $scope.closeAll();
        $scope.endDated2 = true;
	}
	
	//触发租期总长日期开始控件
	$scope.beginDate3 = function($event){
		$event.preventDefault();
        $event.stopPropagation();
        $scope.closeAll();
        $scope.beginDated3 = true;
	}
	//触发租期总长日期结束控件
	$scope.endDate3 = function($event){
		$event.preventDefault();
        $event.stopPropagation();
        $scope.closeAll();
        $scope.endDated3 = true;
	}

    $scope.dateOptions = {
        format: 'yyyy-MM-dd',
        startingDay: 1,
        class: 'datepicker'
    };

    $scope.format = 'yyyy-MM-dd';

    $scope.today();
	
	//监控日期变化
    $scope.$watch('startDate1', function(newValue, oldValue) {
        if ("[object Date]" === Object.prototype.toString.call(newValue)) {
            $scope.$emit('contractDate.start', moment(newValue).format('yyyy-MM-dd'))
        } else if (null === newValue) {
            $scope.$emit('contractDate.start', '')
        }
    });
    $scope.$watch('overDate1', function(newValue, oldValue) {
        if ("[object Date]" === Object.prototype.toString.call(newValue)) {
            $scope.$emit('contractDate.end', moment(newValue).format('YYYY-MM-DD'))
        } else if (null === newValue) {
            $scope.$emit('contractDate.end', '')
        }
    });
    $scope.$watch('startDate2', function(newValue, oldValue) {
        if ("[object Date]" === Object.prototype.toString.call(newValue)) {
            $scope.$emit('contractDate.start', moment(newValue).format('YYYY-MM-DD'))
        } else if (null === newValue) {
            $scope.$emit('contractDate.start', '')
        }
    });
    $scope.$watch('overDate2', function(newValue, oldValue) {
        if ("[object Date]" === Object.prototype.toString.call(newValue)) {
            $scope.$emit('contractDate.end', moment(newValue).format('YYYY-MM-DD'))
        } else if (null === newValue) {
            $scope.$emit('contractDate.end', '')
        }
    });
    $scope.$watch('startDate3', function(newValue, oldValue) {
        if ("[object Date]" === Object.prototype.toString.call(newValue)) {
            $scope.$emit('contractDate.start', moment(newValue).format('YYYY-MM-DD'))
        } else if (null === newValue) {
            $scope.$emit('contractDate.start', '')
        }
    });
    $scope.$watch('overDate3', function(newValue, oldValue) {
        if ("[object Date]" === Object.prototype.toString.call(newValue)) {
            $scope.$emit('contractDate.end', moment(newValue).format('YYYY-MM-DD'))
        } else if (null === newValue) {
            $scope.$emit('contractDate.end', '')
        }
    });
    
});
